<?php

namespace app\controllers\h5\my;

use app\controllers\h5\base\H5Controller;
use app\models\group\Group;
use app\models\group\GroupFans;
use app\models\group\GroupList;
use app\models\marketing\MarketingGroup;
use app\models\marketing\MarketingGroupLevel;
use app\models\order\Order;
use app\models\user\User;

/**
 * Class GroupController
 * @package app\controllers\h5\my
 */
class GroupController extends H5Controller
{
    public function actionCreate()
    {
        $page = intval($this->post('page', $this->page));
        $pageSize = intval($this->post('pageSize', $this->pageSize));

        $query = Group::find()
            ->where(['>=', 'status', 1])
            ->andWhere(['user_id' => $this->user->id]);

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $pageSize);

        $list = $query->orderBy(['id' => SORT_DESC])
            ->offset(($page - 1) * $pageSize)
            ->limit($pageSize)
            ->with([
                'order', 'product', 'product.mainImage',
                'fans', 'fans.user', 'fans.order'
            ])
            ->asArray()
            ->all();

        // foreach ($list as $key => &$item) {
        //     // not payment
        //     $group_pay = $item['order']['status'];
        //     if (in_array($group_pay, [Order::$statusMapping['NON_PAYMENT'], Order::$statusMapping['CANCEL']])) {
        //         unset($list[$key]);
        //     }
        //     // fans not payment
        //     foreach ($item['fans'] as $i => $v) {
        //         $fans_pay = $v['order']['status'];
        //         if (in_array($fans_pay, [Order::$statusMapping['NON_PAYMENT'], Order::$statusMapping['CANCEL']])) {
        //             unset($item['fans'][$i]);
        //         }
        //     }
        // }

        $quantity = MarketingGroup::getLastOne();
        $quantity = $quantity->quantity;

        return $this->successEncode([
            $this->list => $list,
            $this->totalCount => (int)$totalCount,
            $this->totalPage => $totalPage,
            $this->currentPage => $page,
            'quantity' => $quantity
        ]);
    }

    public function actionJoin()
    {
        $page = intval($this->post('page', $this->page));
        $pageSize = intval($this->post('pageSize', $this->pageSize));

        $fans = GroupFans::find()
            ->where(['user_id' => $this->user->id])
            ->andWhere(['status' => 1])
            ->asArray()
            ->all();
        $ids = [];
        foreach ($fans as $item) {
            // not payment
            // $order = Order::findOne(['id' => $item['order_id']]);
            // if (in_array($order->status, [Order::$statusMapping['NON_PAYMENT'], Order::$statusMapping['CANCEL']])) {
            //     continue;
            // }

            // myself
            $myself = Group::findOne(['id' => $item['group_id']]);
            if (!$myself || $myself->user_id == $this->user->id) {
                continue;
            }
            array_push($ids, $item['group_id']);
        }

        $query = Group::find()
            ->where(['>=', 'status', 1])
            ->andWhere(['id' => $ids]);

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $pageSize);

        $list = $query->orderBy(['id' => SORT_DESC])
            ->offset(($page - 1) * $pageSize)
            ->limit($pageSize)
            ->with([
                'order', 'product', 'product.mainImage',
                'fans', 'fans.user'
            ])
            ->asArray()
            ->all();

        $quantity = MarketingGroup::getLastOne();
        $quantity = $quantity->quantity;

        return $this->successEncode([
            $this->list => $list,
            $this->totalCount => (int)$totalCount,
            $this->totalPage => $totalPage,
            $this->currentPage => $page,
            'quantity' => $quantity
        ]);
    }

    // pool total
    public function actionGetPoolTotal()
    {
        $last_total = GroupList::find()
            ->orderBy(['id' => SORT_DESC])
            ->limit(1)
            ->one();

        // vip
        $vip = MarketingGroupLevel::find()
            ->orderBy(['id' => SORT_ASC])
            ->asArray()
            ->all();

        return $this->successEncode([
            'total' => $last_total->total,
            'add' => $last_total->add,
            'vip' => $vip
        ]);
    }

    // level
    public function actionLevel()
    {
        $user = User::find()
            ->where(['id' => $this->user->id])
            ->with(['level0', 'groupLevel0'])
            ->select(['level', 'group_level', 'id'])
            ->asArray()
            ->one();

        // recommend total up big
        $num = MarketingGroup::getLastOne();
        $num = $num->vip2_recommend_finish_group_total;

        // vip
        $vip = MarketingGroupLevel::find()
            ->orderBy(['id' => SORT_ASC])
            ->asArray()
            ->all();

        // performance
        $first_children = User::find()
            ->where(['referrer_id' => $user['id']])
            ->select(['id'])
            ->column();
        $all_users = array_merge([$user['id']], $first_children);
        $performance_total = Order::find()
            ->where(['user_id' => $all_users])
            ->andWhere(['!=', 'status', Order::$statusMapping['NON_PAYMENT']])
            ->andWhere(['!=', 'status', Order::$statusMapping['CANCEL']])
            ->sum('total_pay');

        return $this->successEncode([
            'user' => $user,
            'num' => $num,
            'vip' => $vip,
            'performance' => $performance_total
        ]);
    }

    // pool list
    public function actionPoolList()
    {
        $page = intval($this->post('page', $this->page));
        $pageSize = intval($this->post('pageSize', $this->pageSize));

        $query = GroupList::find();

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $pageSize);

        $list = $query->orderBy(['id' => SORT_DESC])
            ->offset(($page - 1) * $pageSize)
            ->limit($pageSize)
            ->asArray()
            ->all();

        // return total
        foreach ($list as $key => &$value) {
            if ($key + 1 == count($list)) {
                $value['return'] = $value['add'] - $value['total'];
                $value['before'] = 0;
            } else {
                $before_total = $list[$key + 1]['total'];
                $value['return'] = $before_total + $value['add'] - $value['total'];
                $value['before'] = $before_total;
            }
        }

        return $this->successEncode([
            $this->list => $list,
            $this->totalCount => (int)$totalCount,
            $this->totalPage => $totalPage,
            $this->currentPage => $page
        ]);
    }
}